﻿using DevExpress.XtraEditors;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmOrg_Reg : XtraForm
    {
        public FrmOrg_Reg()
        {
            InitializeComponent();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.No;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.OrgCode) && SystemInfo.DbData.IsOpen)
            {
                SystemInfo.DbData.ExecuteSql(string.Format("delete From KTSYS_ORG_RGN where ORG_CODE = '{0}'", this.OrgCode));
                foreach (ListItemRegion region in this.ListREGIONAL.Items)
                {
                    DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("ORGCODE", this.OrgCode), new InputDataParam("RGNCODE", region.RGN_CODE) });
                    SystemInfo.DbData.ExecuteSql("insert into KTSYS_ORG_RGN values(:ORGCODE,:RGNCODE)", parms);
                }
                base.Hide();
            }
        }
        public void Edit(string Code)
        {
            this.OrgCode = Code;
            this.getTreeData();
            this.getSelectedReg();
            base.ShowDialog();
        }

        private void FrmOrg_Reg_Load(object sender, EventArgs e)
        {
        }

        private void getSelectedReg()
        {
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("ORGCODE", this.OrgCode) });
            string commandtext = "select MXHLY_SYS_REGIONAL.RGN_CODE,MXHLY_SYS_REGIONAL.RGN_NAME from KTSYS_ORG_RGN,MXHLY_SYS_REGIONAL where KTSYS_ORG_RGN.RGN_CODE=MXHLY_SYS_REGIONAL.RGN_CODE and KTSYS_ORG_RGN.ORG_CODE=:ORGCODE";
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt))
            {
                this.ListREGIONAL.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItemRegion item = new ListItemRegion
                    {
                        RGN_CODE = dt.Rows[i]["RGN_CODE"].ToString(),
                        RGN_NAME = dt.Rows[i]["RGN_NAME"].ToString()
                    };
                    this.ListREGIONAL.Items.Add(item);
                }
            }
        }

        private void getTreeData()
        {
            if (this.OrgCode.Length >= 8)
            {
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("ORGCODE", this.OrgCode.Substring(0, 8)) });
                string commandtext = "select RGN_CODE from KTSYS_ORG_RGN Where ORG_CODE=:ORGCODE";
                DataTable dt = new DataTable();
                if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt))
                {
                    string str2 = string.Empty;
                    string str3 = string.Empty;
                    if ((dt != null) && (dt.Rows.Count > 0))
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            str3 = str3 + string.Format(" RGN_CODE Like '{0}%' OR", row["RGN_CODE"].ToString());
                        }
                        if (str3 != string.Empty)
                        {
                            str3 = str3.ToString().Substring(0, str3.Length - 3);
                            str2 = string.Format("select RGN_ID,RGN_CODE,RGN_NAME,RGN_PARENT_ID from MXHLY_SYS_REGIONAL where {0}", str3);
                        }
                    }
                    DataTable table2 = new DataTable();
                    if (str2 != string.Empty)
                    {
                        SystemInfo.DbData.Retrieve(str2, ref table2);
                    }
                    this.treeREGIONAL.DataSource = table2;
                    this.treeREGIONAL.RefreshDataSource();
                }
            }
        }
        private bool IsExistRegion()
        {
            foreach (ListItemRegion region in this.ListREGIONAL.Items)
            {
                if (region.RGN_CODE == this.treeREGIONAL.FocusedColumn.FieldName)
                {
                    return true;
                }
            }
            return false;
        }

        private void toDelete_Click(object sender, EventArgs e)
        {
            for (int i = this.ListREGIONAL.SelectedItems.Count; i > 0; i--)
            {
                this.ListREGIONAL.Items.Remove(this.ListREGIONAL.SelectedItems[i - 1]);
            }
        }

        private void toSelect_Click(object sender, EventArgs e)
        {
            if ((this.treeREGIONAL.FocusedNode != null) && !this.IsExistRegion())
            {
                ListItemRegion item = new ListItemRegion
                {
                    RGN_CODE = this.treeREGIONAL.FocusedNode.GetValue("RGN_CODE").ToString(),
                    RGN_NAME = this.treeREGIONAL.FocusedNode.GetValue("RGN_NAME").ToString()
                };
                this.ListREGIONAL.Items.Add(item);
            }
        }

        // Nested Types
        private class ListItemRegion
        {
            // Fields
            private string _RGN_CODE;
            private string _RGN_NAME;

            // Methods
            public ListItemRegion()
            {
            }

            public ListItemRegion(string RGN_CODE, string RGN_NAME)
            {
                this._RGN_CODE = RGN_CODE;
                this._RGN_NAME = RGN_NAME;
            }

            public override string ToString()
            {
                return this._RGN_NAME;
            }

            // Properties
            public string RGN_CODE
            {
                get
                {
                    return this._RGN_CODE;
                }
                set
                {
                    this._RGN_CODE = value;
                }
            }

            public string RGN_NAME
            {
                get
                {
                    return this._RGN_NAME;
                }
                set
                {
                    this._RGN_NAME = value;
                }
            }
        }
    }
}
